package org.fxml.managers {
	import org.fxml.utils.BitmapUtil;

	import flash.display.Bitmap;
	import flash.utils.Dictionary;
	import flash.utils.Proxy;
	import flash.utils.flash_proxy;

	/**
	 * @author jordandoczy
	 * @private
	 */
	dynamic public class AssetManager extends Proxy {
		
		protected var _assets:Dictionary = new Dictionary();
		
		override flash_proxy function callProperty(methodName:*, ... args):* {
			return this[methodName].apply(this, args);
	    }
		
		override flash_proxy function getProperty(name:*):*{
			return getAsset(name) || null;
		}
		
		override flash_proxy function hasProperty(name:*):Boolean{
			return contains(name);
		}
		
		override flash_proxy function setProperty(name:*, value:*):void{
			addItem(name, value);	
		}
		
		public function contains(key:String):Boolean{
			return _assets[key] != null;
		}
		
		public function getAsset(key:String):*{
			var asset:* = _assets[key];
			if(asset is Bitmap) return BitmapUtil.cloneBitmp(asset as Bitmap);
			else return asset;
		}
		
		public function addItem(key:String, asset:*):void{
			_assets[key] = asset;
		}
	}
}